package com.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/**
 * JAVA反射技术
 * 
 * @author Jelly QQ:136179492
 * 
 */
public class ReflectTester {
	/**
	 * 通过一个对象利用反射技术将其复制
	 * 
	 * @param 参数object
	 * @return 复制的Object对象
	 * @throws Exception
	 */
	public Object copy(Object object) throws Exception {
		Class<?> classType = object.getClass();
		Object obj = classType.getConstructor(new Class[] {}).newInstance(
				new Object[] {});
		Field[] fields = classType.getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();
			String firstLetter = name.substring(0, 1).toUpperCase();
			String getMethodName = "get" + firstLetter + name.substring(1);
			String setMethodName = "set" + firstLetter + name.substring(1);
			Method getMethod = classType.getMethod(getMethodName,
					new Class[] {});
			Method setMethod = classType.getMethod(setMethodName,
					new Class[] { field.getType() });
			Object value = getMethod.invoke(object, new Object[] {});
			System.out.println(value);
			setMethod.invoke(obj, new Object[] { value });
		}
		return obj;
	}
	
	
	public static void prinltnCopy(Object g,Object s)throws Exception{
		List<String> listg = findFieldNames(g);
		List<String> lists = findFieldNames(s);
		for(String name : lists){
			if(listg.contains(name)){
				String firstLetter = name.substring(0, 1).toUpperCase();
				System.out.println("s.set"+firstLetter + name.substring(1)+"("+"r.get"+firstLetter + name.substring(1)+"()"+");");
			}
		}
	}
	
	public static void prinltnCopy1(Object g,Object s)throws Exception{
		List<String> listg = findFieldNames(g);
		Class<?> classType = s.getClass();
		Object obj = classType.getConstructor(new Class[] {}).newInstance(
				new Object[] {});
		Field[] fields = classType.getDeclaredFields();
		for (Field field : fields) {
			
			if(listg.contains(field.getName())){

				if (field.getGenericType().toString().equals(  
	                    "class java.lang.String")) {
					StringBuffer bf = new StringBuffer();
					String name = field.getName();
					String firstLetter = name.substring(0, 1).toUpperCase();
					String getMethodName = "get" + firstLetter + name.substring(1);
					String setMethodName = "set" + firstLetter + name.substring(1);
					
					bf.append("\r\n");
					bf.append("if(null != result.");
					bf.append(getMethodName);
					bf.append("()){");
					bf.append("\r\n");
					
					bf.append("if(!result.");
					bf.append(getMethodName);
					bf.append("().equals(liveresult.");
					bf.append(getMethodName);
					bf.append("())){");
					bf.append("\r\n");
					
					bf.append("liveresult.");
					bf.append(setMethodName);
					bf.append("(result.");
					bf.append(getMethodName);
					bf.append("());");
					bf.append("\r\n");
					
					bf.append("change = true;");
					bf.append("\r\n");
					
					bf.append("}");
					bf.append("\r\n");
					bf.append("}");
					bf.append("\r\n");
					System.out.println(bf.toString());
					
				}else if (field.getGenericType().toString().equals(  
	                    "class java.lang.Integer") || field.getGenericType().toString().equals(  
	                            "int")) {
					StringBuffer bf = new StringBuffer();
					String name = field.getName();
					String firstLetter = name.substring(0, 1).toUpperCase();
					String getMethodName = "get" + firstLetter + name.substring(1);
					String setMethodName = "set" + firstLetter + name.substring(1);
					
					bf.append("\r\n");
					bf.append("if(result.");
					bf.append(getMethodName);
					bf.append("() != liveresult.");
					bf.append(getMethodName);
					bf.append("()){");
					bf.append("\r\n");
					
					bf.append("liveresult.");
					bf.append(setMethodName);
					bf.append("(result.");
					bf.append(getMethodName);
					bf.append("());");
					bf.append("\r\n");
					
					bf.append("change = true;");
					bf.append("\r\n");
					
					bf.append("}");
					bf.append("\r\n");
					System.out.println(bf.toString());
					
					
				}
			}
			
			
		}
		/*for(String name : lists){
			if(listg.contains(name)){
				String firstLetter = name.substring(0, 1).toUpperCase();
				System.out.println("s.set"+firstLetter + name.substring(1)+"("+"r.get"+firstLetter + name.substring(1)+"()"+");");
			}
		}*/
	}
	
	
	public static List<String> findFieldNames(Object g)throws Exception{
		List<String> list = new ArrayList<String>();
		Class<?> classType = g.getClass();
		Field[] fields = classType.getDeclaredFields();
		for (Field field : fields) {
			list.add(field.getName());
		}
		return list;
	}
	

	public static void main(String[] args) throws Exception,
			InvocationTargetException {
		List<String> list = findFieldNames(new Customer("jelly", 23));
		//System.out.println();
	/*	ReflectTester test = new ReflectTester();
		Customer customer = new Customer("jelly", 23);
		Customer obj = (Customer) test.copy(customer);
		System.out.println(obj.getName() + obj.getAge());*/
	}
}

class Customer {
	public Customer() {

	}

	public Customer(String name, int age) {
		this.name = name;
		this.age = age;
	}

	private long id;
	private String name;
	private int age;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

